<?php

class Admin_Model_User extends Zend_Db_Table_Abstract
{
    protected $_name = 'user';    
    public function getAllData(){
		$query = $this->select()                        
                        ->from($this->_name);
        return $this->fetchAll($query);
    }    
    public function getDataById($id){
        $query = $this->select()                        
                        ->from($this->_name)
                        ->where('id = ?',(int)$id);
        return $this->fetchRow($query);
    }
    public function getDataCheckUnique($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from($this->_name);    
        if(isset($data['email']) && $data['email']){
            $query->where("email = '$data[email]'");
        }                     
        if(isset($data['username']) && $data['username']){
            $query->orWhere("username = '$data[username]'");
        }                                                                                        
        return $this->fetchRow($query);
    }
    public function getDataByUsername($username){
        $query = $this->select()
                        ->from($this->_name)
                        ->where('username = ?', $username);
        return $this->fetchRow($query);
    }
    public function getDataByEmail($email){
        $query = $this->select()
                        ->from($this->_name)
                        ->where('email = ?', $email);
        return $this->fetchRow($query);
    }
    public function getDataByMobile($mobile){
        $query = $this->select()
            ->from($this->_name)
            ->where('mobile = ?', $mobile);
        return $this->fetchRow($query);
    }
    public function addData($username, $email, $password, $status){        
        $data = array('username'    =>$username,
                'email'             =>$email,
                'password'          =>md5($password),
                'status'            =>$status
                );                
        $idAdd = $this->insert($data);        
        return $idAdd;
    }
    public function updateData($id = '',$data){                
            $data = array('username'    =>$data['username'],
                    'email'             =>$data['email'],            
                    'fullname'          =>$data['fullname'],
                    'mobile'            =>$data['mobile'],
                    'address'           =>$data['address'],
                    'status'            =>$data['status'],
                    'gold'              =>$data['gold'],
                    'qty'               =>$data['qty']);                    
        return $this->update($data, 'id = '.(int)$id);
    }           
    public function updateGold($id = '',$data){
        $data = array('gold'            =>$data['gold']);                    
        return $this->update($data, 'id = '.(int)$id);
    }
    public function inceaseQty($id = '',$qty){                
        $data = array('qty'=>$qty);                    
        return $this->update($data, 'id = '.(int)$id);
    }     
    public function deleteData($id){
        $this->delete('id = '.(int)$id);
    }    
    public function updateStatus($id){
        $this->update(array('status'=>'1'),'id = '.$id);                        
    }
    public function updatePass($id, $password){
        $data['password'] = md5($password);
        return $this->update($data,'id='.(int)$id);
    }               
}